﻿using HalconDotNet;
using MachineVision.Core.Extensions;
using MachineVision.Core.TemplateMatch.Shared;
using Prism.Mvvm;
using System.Diagnostics;

namespace MachineVision.Core.OCR
{
    public class QrCodeService : BindableBase
    {
        public QrCodeService()
        {
            OnInitize();
            Roi = new ROIParameter();
            Info = new()
            {
                Name = "find_data_code_2d",
                Description = "Detect and read 2D data code symbols in an image or train the 2D data code model.",
                Parameters = new List<MethodParameter>()
                {
                    new MethodParameter(){Name="Image",Description = "XLD contours that surround the successfully decoded data code symbols. The order of the contour points reflects the orientation of the detected symbols. The contours begin in the top left corner (see 'orientation' in the reference of get_data_code_2d_results) and continue clockwise."},
                    new MethodParameter(){Name="SymbolXLDs",Description = "XLD contours that surround the successfully decoded data code symbols. The order of the contour points reflects the orientation of the detected symbols. The contours begin in the top left corner (see 'orientation' in the reference of get_data_code_2d_results) and continue clockwise.\r\n"},
                    new MethodParameter(){Name="DataCodeHandle",Description = "Handle of the 2D data code model."},
                    new MethodParameter(){Name="GenParamName",Description = "Names of (optional) parameters for controlling the behavior of the operator."},
                    new MethodParameter(){Name="GenParamValue",Description = "Values of the optional generic parameters."},
                    new MethodParameter(){Name="ResultHandles",Description = "Handles of all successfully decoded 2D data code symbols."},
                    new MethodParameter(){Name="DecodedDataStrings",Description = "Decoded data strings of all detected 2D data code symbols in the image."},
                },
                Preducessors = new List<string>()
                {
                    "create_data_code_2d_model",
                    "read_data_code_2d_model",
                    "set_data_code_2d_param"
                }
            };
        }
        HObject ho_SymbolXLDs = null;
        HTuple hv_Message = new HTuple();
        HTuple hv_DataCodeHandle = new HTuple();
        HTuple hv_ResultHandles = new HTuple();
        HTuple hv_DecodedDataStrings = new HTuple();

        private void OnInitize()
        {
            hv_DataCodeHandle.Dispose();
            HOperatorSet.CreateDataCode2dModel("QR Code", new HTuple(), new HTuple(), out hv_DataCodeHandle);
        }

        public OcrMatchResult Run(HObject image)
        {
            var act = () =>
            {
                ho_SymbolXLDs?.Dispose();
                hv_ResultHandles?.Dispose();
                hv_DecodedDataStrings?.Dispose();
                HOperatorSet.FindDataCode2d(image.ReduceDomain(Roi), out ho_SymbolXLDs, hv_DataCodeHandle, new HTuple(), new HTuple(), out hv_ResultHandles, out hv_DecodedDataStrings);
                if (hv_DecodedDataStrings.Length <= 0) throw new Exception("未识别到二维码");
            };
            var timespan = act.SetTimer();
            return new OcrMatchResult()
            {
                IsSuccess = true,
                Message = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}：识别耗时：{timespan}ms,识别结果：{hv_DecodedDataStrings.ToString()}",
                TimeSpan = timespan
            };
        }

        public MethodInfo Info { get; set; }
        public ROIParameter Roi { get; set; }
    }
}
